#include "PrecompiledHeaders.h"
#include "MenuInGame.h"
#include "ReplayState.h"

/*
 * Grab state pointers
 */
void MenuInGame::getStatePointers()
{
    options = (MenuOptions *)((MenuState *)MenuState::findByName("options"));
}

std::string MenuInGame::getGhostReplay()
{
    if(USE_GHOST) return MenuState::resources->scores->getTopScoreReplay(MenuState::resources->current_level->name);
    else return ""; 
}

void MenuInGame::enter(void)
{
    // Shows mouse
    CEGUI::MouseCursor::getSingleton().show();
    // Loads the MainMenu.layout script which places all of the main menu buttons on the screen
    layoutHolder = CEGUI::WindowManager::getSingletonPtr()->loadWindowLayout("MenuInGame.layout");
    MenuState::resources->GUISystem->setGUISheet(layoutHolder);
    currentState = lsNORMAL;
    playReplay = false;
    loadingReplay = false;
    ReplayState::resources = MenuState::resources;
    
    switch(layoutType) {
        case SM_LEVEL_COMPLETE: setupSMLevelComplete(); break;
        case SM_GAME_OVER:      setupSMGameOver();      break;
        case SM_FINAL_LEVEL_COMPLETE: setupSMLevelCompleteEndGame(); break;
        case SM_COMPLETED:      setupSMCompleted();     break;
        case QP_LEVEL_COMPLETE: setupQPLevelComplete(); break;
        case QP_FAILED:         setupQPFailed();        break;
        case LOAD_REPLAY:       setupLoadingReplay();   break;
        case LOAD:              setupLoading();         break;
        case PAUSE:             setupPause();           break;
    }
    
    // if display replay behind
    if(playReplay) {
        overlays_enabled = false;
        ReplayState::enter();
    }
}

void MenuInGame::exit(void)
{
    // Need to unsubscribe buttons depending on what menu state
    printf("a\n");
	highScoreAchieved = false;
    printf("b\n");
    CEGUI::MouseCursor::getSingleton().hide();
    printf("c\n");
    CEGUI::WindowManager::getSingleton().destroyAllWindows();
    printf("d\n");
	activeWindows.clear();
    printf("e\n");
    
    switch(layoutType)
    {    
        case SM_LEVEL_COMPLETE:
        case SM_FINAL_LEVEL_COMPLETE:
        case SM_GAME_OVER:
        case SM_COMPLETED:
        case QP_LEVEL_COMPLETE:
        case QP_FAILED:
            ReplayState::exit();
            break;
            
        case LOAD_REPLAY:
        case LOAD:
        case PAUSE:
            break;
    }
    
    /*if(playReplay) {
        ReplayState::exit();
    }*/
    printf("f\n");
}

bool MenuInGame::frameStarted(const Ogre::FrameEvent &evt)
{
    if (MenuState::resources->joystick != NULL)
	{
		if (MenuState::resources->joystick->next()) nextButton();
		else if (MenuState::resources->joystick->previous()) prevButton();
		else if (MenuState::resources->joystick->triggerPressed()) clickButton();
		else if (MenuState::resources->joystick->quit())
		{
		    if (layoutType == PAUSE)
			{
				currentState = lsRESUME;
				unsubscribeButton("EmbeddedPauseButtons/ResumeButton");
				MenuState::popGameState();
			}
			else
			{
				currentState = lsQUIT;
				MenuState::popGameState();
			}
		}
	}
    if(playReplay) {
        return ReplayState::frameStarted(evt);
    }
    return true;
}

bool MenuInGame::frameEnded(const Ogre::FrameEvent &evt)
{
    switch(currentState)
    {
        case lsLOADING:
            // Load the level
            MenuState::resources->ogre_newt = MenuState::resources->current_level->load(
                MenuState::resources->scene_manager,
                MenuState::resources->config, MenuState::resources->skyBox->getSkyBox());
            // Load the BGM
            MenuState::resources->audio->loadRandomBGM();
            MenuState::resources->audio->stopBGM();
            MenuState::resources->audio->stopMenuBGM();
            currentState = lsLOADED;
            break;
            
        case lsLOADED:
            if(loadingReplay) {
                currentState = lsPLAY_REPLAY;
                MenuState::popGameState();
                loadingReplay = false;
            } else {
                currentState = lsNORMAL;
                setupLoaded();
            }
            break;
        
        default:
            break;            
    }
    if(playReplay)
        return ReplayState::frameEnded(evt);
    else
        return true;
}

void MenuInGame::setupSMGameOver()
{
    setWindowImage("MenuInGame/Header", "set:MenuImages image:GameOver");
    setupSMEnd();
}

void MenuInGame::setupSMCompleted()
{
    setWindowImage("MenuInGame/Header", "set:MenuImages image:Completed");
    setupSMEnd();
}

/*
 * Extended from replay state
 */
void MenuInGame::stopPlaying()
{
    // replay replay - reinitialise all variables
    std::vector<Player*>::iterator it; 
    for(it = players.begin(); it != players.end(); it++) {
        (*it)->reset();
    }
    camera->reset();

    time_until_user_can_control = getTimeUntilUserCanControl();
    
    // set up HUD and other overlays
    initialiseHUD();
    initialiseCountdownOverlay();
    initialiseExtraOverlays();
    
    // set up other bits
    stop_playing = false;
    ogre_time = 0;
    ogre_frame_count = 0;
    startPlaying();
}

/*
 * Story mode game over screen
 */
void MenuInGame::setupSMEnd()
{
    playReplay = true;
	int gameScore = PlayState::main_player->getSession()->getGameScore();
    addEmbeddedWindow("EmbeddedHighScores.layout");
    MenuState::resources->scores->setScores("game");
	setWindowText("EmbeddedHighScores/LabelHighScores", "Balls of Fame");
    addEmbeddedWindow("EmbeddedGameScore.layout");
	setWindowText("EmbeddedGameScore/FinalScore", MenuState::resources->scores->formatScore(gameScore));
	setWindowText("EmbeddedGameScore/Ranking", MenuState::resources->scores->getRanking(gameScore));
	setWindowColour("EmbeddedGameScore/Ranking", MenuState::resources->scores->getRankingColour(gameScore));
	addEmbeddedWindow("EmbeddedSMGameOverButtons.layout");
    // Continue Button
    subscribeButton("ContinueButton",
        &MenuInGame::clickContinue,
        &MenuInGame::hoverContinue,
        &MenuInGame::offContinue);
	if (MenuState::resources->scores->isHighScore("maingame", gameScore))
    {	
		highScoreAchieved = true;
        addEmbeddedWindow("EmbeddedHighScoreEntry.layout");
        CEGUI::Window *editBox = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("EmbeddedHighScoreEntry/ScoreEditbox");
		addWinPos(editBox, false);
		subscribeButton("EmbeddedHighScoreEntry/EnterButton",
        &MenuInGame::clickSubmitScore,
        &MenuInGame::hoverSubmitScore,
        &MenuInGame::offSubmitScore);
    } else {
        addEmbeddedWindow("EmbeddedNoHighScore.layout");
    }
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

void MenuInGame::setupLevelComplete()
{
    playReplay = true;
    setWindowImage("MenuInGame/Header", "set:MenuImages image:LevelComplete");
    addEmbeddedWindow("EmbeddedHighScores.layout");
    MenuState::resources->scores->setScores(MenuState::resources->current_level->name);
    addEmbeddedWindow("EmbeddedLevelScore.layout");
	
	setWindowText("EmbeddedLevelScore/Score", MenuState::resources->scores->formatScore(
	    PlayState::getMainPlayersSession()->getLevelScore()));
	setWindowText("EmbeddedLevelScore/LifeBonus", MenuState::resources->scores->formatScore(
	    PlayState::getMainPlayersSession()->getLifeBonus()));
	setWindowText("EmbeddedLevelScore/TimeBonus", MenuState::resources->scores->formatScore(
	    PlayState::getMainPlayersSession()->getTimeBonus()));
	setWindowText("EmbeddedLevelScore/LevelScore", MenuState::resources->scores->formatScore(
	    PlayState::getMainPlayersSession()->getTotalLevelScore()));

    if(PlayState::getMainPlayersSession()->hasAddedHighScore()) {
        // Don't embed anything
        return;
    }
    // If the player can enter a high score
    if(MenuState::resources->scores->isHighScore(
        MenuState::resources->current_level->name, 
        PlayState::getMainPlayersSession()->getTotalLevelScore()))
    {
		highScoreAchieved = true;
        addEmbeddedWindow("EmbeddedHighScoreEntry.layout");
        // Buttons: Add new high score, watch replay, next level, quit to main menu
		CEGUI::Window *editBox = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("EmbeddedHighScoreEntry/ScoreEditbox");
		addWinPos(editBox, false);
		subscribeButton("EmbeddedHighScoreEntry/EnterButton",
            &MenuInGame::clickSubmitScore,
            &MenuInGame::hoverSubmitScore,
            &MenuInGame::offSubmitScore);
        highScoreHold = PlayState::getMainPlayersSession()->getTotalLevelScore();
    }
    // If the player did not achieve a high score
    else {
        addEmbeddedWindow("EmbeddedNoHighScore.layout");
    }
}

void MenuInGame::setupSMLevelCompleteEndGame()
{
    playReplay = true;
    addEmbeddedWindow("EmbeddedTotalScore.layout");
	setWindowText("EmbeddedTotalScore/TotalScore", MenuState::resources->scores->formatScore(PlayState::main_player->getSession()->getGameScore()));
    addEmbeddedWindow("EmbeddedSMLevelCompleteButtons.layout");
    subscribeButton("WatchReplayButton",
        &MenuInGame::clickWatchReplay,
        &MenuInGame::hoverWatchReplay,
        &MenuInGame::offWatchReplay);
	CEGUI::WindowManager::getSingletonPtr()->destroyWindow("EmbeddedSMLevelCompleteButtons/NextLevelButton");
	addEmbeddedWindow("EmbeddedContinueButton.layout");
	subscribeButton("ContinueButton",
        &MenuInGame::clickContinue,
        &MenuInGame::hoverContinue,
        &MenuInGame::offContinue);
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	setupLevelComplete();
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

/*
 * Story mode level complete screen
 */
void MenuInGame::setupSMLevelComplete()
{
    playReplay = true;
	addEmbeddedWindow("EmbeddedTotalScore.layout");
	setWindowText("EmbeddedTotalScore/TotalScore", MenuState::resources->scores->formatScore(PlayState::main_player->getSession()->getGameScore()));
    addEmbeddedWindow("EmbeddedSMLevelCompleteButtons.layout");
    subscribeButton("WatchReplayButton",
        &MenuInGame::clickWatchReplay,
        &MenuInGame::hoverWatchReplay,
        &MenuInGame::offWatchReplay);
    subscribeButton("EmbeddedSMLevelCompleteButtons/NextLevelButton",
        &MenuInGame::clickNextLevel,
        &MenuInGame::hoverNextLevel,
        &MenuInGame::offNextLevel);
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	setupLevelComplete();
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

/*
 * Quick play level completed screen
 */
void MenuInGame::setupQPLevelComplete()
{
    playReplay = true;
    addEmbeddedWindow("EmbeddedQPLevelCompleteButtons.layout");
    subscribeButton("WatchReplayButton",
        &MenuInGame::clickWatchReplay,
        &MenuInGame::hoverWatchReplay,
        &MenuInGame::offWatchReplay);
    subscribeButton("TryAgainButton",
        &MenuInGame::clickTryAgain,
        &MenuInGame::hoverTryAgain,
        &MenuInGame::offTryAgain);
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	setupLevelComplete();
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

/*
 * Quick play level failed screen
 */
void MenuInGame::setupQPFailed()
{
    playReplay = true;
    setWindowImage("MenuInGame/Header", "set:MenuImages image:LevelFailed");
    addEmbeddedWindow("EmbeddedHighScores.layout");
    MenuState::resources->scores->setScores(MenuState::resources->current_level->name);
    //addEmbeddedWindow("EmbeddedTryAgain.layout");
	addEmbeddedWindow("EmbeddedQPFailedButtons.layout");
	subscribeButton("TryAgainButton",
		&MenuInGame::clickTryAgain,
        &MenuInGame::hoverTryAgain,
        &MenuInGame::offTryAgain);
    // Buttons: Add new high score, watch replay, next level, quit to main menu
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

void MenuInGame::setupLoadingReplay()
{
    loadingReplay = true;
    // Hides mouse
    CEGUI::MouseCursor::getSingleton().hide();
    currentState = lsLOADING;
    setWindowImage("MenuInGame/Background", "set:MenuBackground2 image:LevelSelectBackground");
    setWindowImage("MenuInGame/Header", "set:MenuImages image:Loading");
    addEmbeddedWindow("EmbeddedLevelImage.layout");
    addEmbeddedWindow("EmbeddedLevelLoadingInfo.layout");
	setWindowImage("EmbeddedLevelImage/LevelImage", "set:Levels image:" + MenuState::resources->current_level->hover_image);
    setWindowText("EmbeddedLevelLoadingInfo/LabelLevelName", MenuState::resources->current_level->level_name);
    setWindowText("EmbeddedLevelLoadingInfo/TimeLimit", formatTime(MenuState::resources->current_level->seconds_for_level));
    setWindowText("EmbeddedLevelLoadingInfo/TopScore", MenuState::resources->scores->getTopScore(MenuState::resources->current_level->name));
}

/*
 * Level loading screen
 */
void MenuInGame::setupLoading()
{
    // Hides mouse
    CEGUI::MouseCursor::getSingleton().hide();
    currentState = lsLOADING;
    setWindowImage("MenuInGame/Background", "set:MenuBackground2 image:LevelSelectBackground");
    setWindowImage("MenuInGame/Header", "set:MenuImages image:Loading");
    addEmbeddedWindow("EmbeddedLevelImage.layout");
    addEmbeddedWindow("EmbeddedLevelLoadingInfo.layout");
	setWindowImage("EmbeddedLevelImage/LevelImage", "set:Levels image:" + MenuState::resources->current_level->hover_image);
    setWindowText("EmbeddedLevelLoadingInfo/LabelLevelName", MenuState::resources->current_level->level_name);
    setWindowText("EmbeddedLevelLoadingInfo/TimeLimit", formatTime(MenuState::resources->current_level->seconds_for_level));
    setWindowText("EmbeddedLevelLoadingInfo/TopScore", MenuState::resources->scores->getTopScore(MenuState::resources->current_level->name));
    // Sets and subscribes the checkbox
	CEGUI::Checkbox *chkboxGhost = (CEGUI::Checkbox*)CEGUI::WindowManager::getSingleton().getWindow("EmbeddedLevelLoadingInfo/GhostModeCheckbox");
	chkboxGhost->subscribeEvent(CEGUI::Checkbox::EventCheckStateChanged, CEGUI::Event::Subscriber(&MenuInGame::clickGhostMode, this));
	chkboxGhost->setSelected(USE_GHOST);
	addWinPos(chkboxGhost, false);
    // Add checkbox event for ghost selection (PlayState::USE_GHOST = true/false)
}

bool MenuInGame::clickGhostMode(const CEGUI::EventArgs &e)
{
	MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
	USE_GHOST = ((CEGUI::Checkbox*)((const CEGUI::WindowEventArgs&)e).window)->isSelected();
	return true;
}

/*
 * Add loaded buttons
 */
void MenuInGame::setupLoaded()
{
    // Shows mouse
    CEGUI::MouseCursor::getSingleton().show();
    setWindowImage("MenuInGame/Header", "set:MenuImages image:Ready");
    addEmbeddedWindow("EmbeddedLoadButtons.layout");
	subscribeButton("EmbeddedLoadButtons/PlayButton",
        &MenuInGame::clickPlay,
        &MenuInGame::hoverPlay,
        &MenuInGame::offPlay);
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	// Initialises the menu index
    winIterator = activeWindows.begin();
}

/*
 * Setup pause menu layout
 */
 void MenuInGame::setupPause()
 {
    MenuState::resources->audio->pauseBGM();
    setWindowImage("MenuInGame/Header", "set:MenuImages image:Paused");
    addEmbeddedWindow("EmbeddedPauseButtons.layout");
	// Buttons: Add new high score, watch replay, next level, quit to main menu
    subscribeButton("EmbeddedPauseButtons/ResumeButton",
        &MenuInGame::clickResume,
        &MenuInGame::hoverResume,
        &MenuInGame::offResume);
	subscribeButton("EmbeddedPauseButtons/OptionsButton",
        &MenuInGame::clickOptions,
        &MenuInGame::hoverOptions,
        &MenuInGame::offOptions);
    subscribeButton("QuitButton",
        &MenuInGame::clickQuit,
        &MenuInGame::hoverQuit,
        &MenuInGame::offQuit);
	// Initialises the menu index
    winIterator = activeWindows.begin();
 }

Ogre::String MenuInGame::formatTime(int timeLimit)
{
	int mins = (int)(timeLimit / 60);
	int secs = timeLimit - (mins * 60);
	std::stringstream smins, ssecs;
	smins.fill('0');
	smins.width(2);
	smins << mins;
    ssecs.fill('0');
	ssecs.width(2);
	ssecs << secs;
	return ("" + smins.str() + ":" + ssecs.str());
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////// EVENT HANDLER FUNCTIONS ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Play
bool MenuInGame::clickPlay(const CEGUI::EventArgs &e)
{
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("EmbeddedLoadButtons/PlayButton");
    MenuState::popGameState();
    return true;
}

bool MenuInGame::hoverPlay(const CEGUI::EventArgs &e)
{
    setWindowText("EmbeddedLoadButtons/PlayButton", "[play]");
    return true;
}

bool MenuInGame::offPlay(const CEGUI::EventArgs &e)
{
    setWindowText("EmbeddedLoadButtons/PlayButton", "play");
    return true;
}

// Submit Score
bool MenuInGame::clickSubmitScore(const CEGUI::EventArgs &e)
{
	return clickSubmitScore();
}

bool MenuInGame::clickSubmitScore()
{
	std::string name = CEGUI::WindowManager::getSingleton().getWindow("EmbeddedHighScoreEntry/ScoreEditbox")->getText().c_str();
	if (name.length() == 0) return false;
	MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
	//PlayState::main_player->getSession()->setHighScoreAdded();
	PlayState::getMainPlayersSession()->setHighScoreAdded();
    unsubscribeButton("EmbeddedHighScoreEntry/EnterButton");
	if ((layoutType == SM_LEVEL_COMPLETE) || (layoutType == QP_LEVEL_COMPLETE) || (layoutType == SM_FINAL_LEVEL_COMPLETE))
	{
		MenuState::resources->scores->addNewScore(MenuState::resources->current_level->name,
			highScoreHold, name, PlayState::main_player->saveReplay());
		MenuState::resources->scores->setScores(MenuState::resources->current_level->name);
	}
	else
	{
		MenuState::resources->scores->addNewScore("game", PlayState::main_player->getSession()->getGameScore(),
			name, PlayState::main_player->saveReplay());
		MenuState::resources->scores->setScores("game");
	}
	CEGUI::WindowManager::getSingleton().destroyWindow("EmbeddedHighScoreEntry/ScoreEditbox");
	CEGUI::WindowManager::getSingleton().destroyWindow("EmbeddedHighScoreEntry/EnterButton");
	activeWindows.pop_back();
	activeWindows.pop_back();
	winIterator = activeWindows.begin();
	moveMouse();
	return true;
}

bool MenuInGame::hoverSubmitScore(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedHighScoreEntry/EnterButton", "[submit]");
	return true;
}

bool MenuInGame::offSubmitScore(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedHighScoreEntry/EnterButton", "submit");
	return true;
}

// Quit
bool MenuInGame::clickQuit(const CEGUI::EventArgs &e)
{
	unsubscribeButton("QuitButton");
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    currentState = lsQUIT;
	if(PlayState::getMainPlayersSession()) PlayState::getMainPlayersSession()->resetHighScoreAdded();
    MenuState::popGameState();
    return true;
}

bool MenuInGame::hoverQuit(const CEGUI::EventArgs &e)
{
    setWindowText("QuitButton", "[quit]");
    return true;
}

bool MenuInGame::offQuit(const CEGUI::EventArgs &e)
{
    setWindowText("QuitButton", "quit");
    return true;
}

// Continue
bool MenuInGame::clickContinue(const CEGUI::EventArgs &e)
{
	MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
	unsubscribeButton("ContinueButton");
	PlayState::getMainPlayersSession()->resetHighScoreAdded();
	MenuState::popGameState();
	return true;
}

bool MenuInGame::hoverContinue(const CEGUI::EventArgs &e)
{
	setWindowText("ContinueButton", "[continue]");
    return true;
}

bool MenuInGame::offContinue(const CEGUI::EventArgs &e)
{
	setWindowText("ContinueButton", "continue");
    return true;
}

// Watch Replay
bool MenuInGame::clickWatchReplay(const CEGUI::EventArgs &e)
{
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("WatchReplayButton");
    MenuState::pushGameState(MenuState::findByName("replay"));
    return true;
}

bool MenuInGame::hoverWatchReplay(const CEGUI::EventArgs &e)
{
    setWindowText("WatchReplayButton", "[watch replay]");
    return true;
}
bool MenuInGame::offWatchReplay(const CEGUI::EventArgs &e)
{
    setWindowText("WatchReplayButton", "watch replay");
    return true;
}

// Next Level, for story mode
bool MenuInGame::clickNextLevel(const CEGUI::EventArgs &e)
{
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("EmbeddedSMLevelCompleteButtons/NextLevelButton");
    currentState = lsNEXT_LEVEL;
	PlayState::getMainPlayersSession()->resetHighScoreAdded();
    MenuState::popGameState();
    return true;
}

bool MenuInGame::hoverNextLevel(const CEGUI::EventArgs &e)
{ 
    setWindowText("EmbeddedSMLevelCompleteButtons/NextLevelButton", "[next level]");
    return true;
}

bool MenuInGame::offNextLevel(const CEGUI::EventArgs &e)
{
    setWindowText("EmbeddedSMLevelCompleteButtons/NextLevelButton", "next level");
    return true;
}

// Try Again
bool MenuInGame::clickTryAgain(const CEGUI::EventArgs &e)
{
    printf("TRYING AGAIN\n");
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("TryAgainButton");
    currentState = lsREPLAY_LEVEL;
	PlayState::getMainPlayersSession()->resetHighScoreAdded();
    MenuState::popGameState();
    return true;
}

bool MenuInGame::hoverTryAgain(const CEGUI::EventArgs &e)
{
	setWindowText("TryAgainButton", "[try again?]");
    return true;
}

bool MenuInGame::offTryAgain(const CEGUI::EventArgs &e)
{
	setWindowText("TryAgainButton", "try again?");
    return true;
}

// Resume
bool MenuInGame::clickResume(const CEGUI::EventArgs &e)
{
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
    MenuState::resources->audio->playBGM();
	unsubscribeButton("EmbeddedPauseButtons/ResumeButton");
	currentState = lsRESUME;
	MenuState::popGameState();
	return true;
}
bool MenuInGame::hoverResume(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedPauseButtons/ResumeButton", "[resume]");
	return true;
}
bool MenuInGame::offResume(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedPauseButtons/ResumeButton", "resume");
	return true;
}

// Options
bool MenuInGame::clickOptions(const CEGUI::EventArgs &e)
{
    MenuState::resources->audio->playSource(AUDIO_MENU_CLICK);
	unsubscribeButton("EmbeddedPauseButtons/OptionsButton");
	options->setBGTransparent();
	MenuState::pushGameState(MenuState::findByName("options"));
	return true;
}

bool MenuInGame::hoverOptions(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedPauseButtons/OptionsButton", "[options]");
	return true;
}

bool MenuInGame::offOptions(const CEGUI::EventArgs &e)
{
	setWindowText("EmbeddedPauseButtons/OptionsButton", "options");
	return true;
}

bool MenuInGame::keyPressed(const OIS::KeyEvent & e)
{
    CEGUI::System *sys = CEGUI::System::getSingletonPtr();
    sys->injectKeyDown(e.key);
    sys->injectChar(e.text);
	bool activeEditbox = isEditboxActive();

	switch (e.key)
    {
        case OIS::KC_ESCAPE:
            if (layoutType == PAUSE)
			{
				currentState = lsRESUME;
				unsubscribeButton("EmbeddedPauseButtons/ResumeButton");
				MenuState::popGameState();
			}
			else
			{
				currentState = lsQUIT;
				MenuState::popGameState();
			}
            break;
		case OIS::KC_LEFT:
			if (!activeEditbox)
				prevButton();
			break;
		case OIS::KC_UP:
			if (!activeEditbox)
				prevButton();
			break;
		case OIS::KC_RIGHT:
			if (!activeEditbox)
				nextButton();
			break;
		case OIS::KC_DOWN:
			if (!activeEditbox)
				nextButton();
			break;
		case OIS::KC_RETURN:
			switch (layoutType)
			{
				case SM_LEVEL_COMPLETE:
				case SM_GAME_OVER:
				case SM_COMPLETED:
				case QP_LEVEL_COMPLETE:
					if (activeEditbox)
					{
						clickSubmitScore();
						break;
					}
					clickButton();
					break;
				default:
					clickButton();
					break;
			}
			break;
        default:
            break;
    }
    return true;
}

bool MenuInGame::isEditboxActive()
{
	switch (layoutType)
	{
		case SM_LEVEL_COMPLETE:
		case SM_GAME_OVER:
		case SM_COMPLETED:
		case QP_LEVEL_COMPLETE:
			if (highScoreAchieved)
			{
				if (!PlayState::getMainPlayersSession()->hasAddedHighScore())
				{
					if ((CEGUI::Window *)CEGUI::WindowManager::getSingleton().getWindow("EmbeddedHighScoreEntry/ScoreEditbox")->isActive())
					{
						return true;
					}
				}
			}
			break;
		default:
			break;
	}
	return false;
}
